let mysql = require("mysql");

const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    port: 3306,
    database: 'student'
})
//封装
function query(sql, callback) {
    db.connect();
    db.query(sql, function (err, result) {
        if (err) {
            console.log(err.message);
            return;
        } callback(result);
    })
    db.end();
}

//查询信息
let Selectsql = 'SELECT * FROM user';
query(Selectsql, function (msg) {
    for (let row in msg) {
        console.log("学生id:" + msg[row].Id + " 姓名:" + msg[row].name + " 性别:" + msg[row].sex);
    }
})

//增加信息
let Addsql = 'INSERT INTO user(name,sex) VALUES("小李","女")';
query(Addsql,function(msg){
    console.log("增加成功");
})

//修改信息
let Updatesql = 'UPDATE user SET name = "马里奥" WHERE Id = 3';
query(Updatesql,function(msg){
    console.log("修改成功");
})

//删除信息
let Deletesql = 'DELETE FROM user WHERE Id = 2';
query(Deletesql,function(msg){
    console.log("删除成功");
})